package com.chainyoung.component_tmtrade.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chainyoung.common.jsoup.interfaces.Text;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.component_tmtrade.R;
import com.chainyoung.component_tmtrade.bean.TradeCurrencyItem;
import com.chainyoung.component_tmtrade.bean.TradeCurrencyItemOrder;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class TradeHistoryRecordAdapter extends BaseQuickAdapter<TradeCurrencyItem, BaseViewHolder> {


    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final String EXPAND = "展开";
    private static final String FOLD = "收起";

    public TradeHistoryRecordAdapter(@Nullable List<TradeCurrencyItem> data) {
        super(R.layout.tmtrade_list_history_agency_item1, data);
    }

    @Override
    protected void convert(final BaseViewHolder helper, TradeCurrencyItem item) {

        helper.setText(R.id.tv_type, item.getType() == 1 ? R.string.buy_in : R.string.sell_out);
        helper.setTextColor(R.id.tv_type, item.getType() == 1 ? Color.parseColor(
                "#09C173") :
                Color.parseColor("#F95A54"));
        ((TextView) helper.getView(R.id.tv_type)).setCompoundDrawablesRelativeWithIntrinsicBounds(item.getType() == 1 ?
                R.drawable.shape_trade_type_mark_green : R.drawable.shape_trade_type_mark_red, 0, 0, 0);
        ((TextView) helper.getView(R.id.tv_type)).setCompoundDrawablePadding(10);

        helper.setText(R.id.tv_time, sdf.format(new Date(item.getAddtime() * 1000)));
        helper.setText(R.id.tv_status, item.getStatus() == 0 ? "正在出售" : item.getStatus() == 1 ? "交易完成" : "已撤销");

        helper.setText(R.id.tv_price, new BigDecimal(item.getPrice()).setScale(6, BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString());
        helper.setText(R.id.tv_total_num, new BigDecimal(item.getNum()).setScale(6, BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString());
        helper.setText(R.id.tv_total_amount, new BigDecimal(item.getAverage_price()).setScale(6, BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString());
        helper.setText(R.id.tv_already, new BigDecimal(item.getDeal()).setScale(6, BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString());
        helper.setText(R.id.tv_remained_num, new BigDecimal(item.getLeft_num()).setScale(6, BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString());

        helper.setText(R.id.txt_price, "委托价格(" + item.getScb().toUpperCase() + ")");
        helper.setText(R.id.txt_total_num, "委托数量(" + item.getXnb().toUpperCase() + ")");
        helper.setText(R.id.txt_total_amount, "成交均价(" + item.getScb().toUpperCase() + ")");
        helper.setText(R.id.txt_already, "已成交(" + item.getXnb().toUpperCase() + ")");
        helper.setText(R.id.txt_remained_num, "未成交(" + item.getXnb().toUpperCase() + ")");


        helper.getView(R.id.tv_expand).setVisibility(item.getStatus() == 2 ? View.INVISIBLE : View.VISIBLE);
        final TextView tvExpand = helper.getView(R.id.tv_expand);
        helper.getView(R.id.tv_expand).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (tvExpand.getText().toString().trim()) {
                    case EXPAND:
                        helper.getView(R.id.rv_list).setVisibility(View.VISIBLE);
                        helper.getView(R.id.iv_fold).setVisibility(View.VISIBLE);
                        helper.setText(R.id.tv_expand, FOLD);
                        ((TextView) helper.getView(R.id.tv_expand)).setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);
                        break;
                    case FOLD:
                        helper.getView(R.id.rv_list).setVisibility(View.GONE);
                        helper.getView(R.id.iv_fold).setVisibility(View.GONE);
                        helper.setText(R.id.tv_expand, EXPAND);
                        ((TextView) helper.getView(R.id.tv_expand)).setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.mipmap.arrow_down_blue, 0);
                        break;
                }

            }
        });

        helper.getView(R.id.iv_fold).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                helper.getView(R.id.rv_list).setVisibility(View.GONE);
                helper.getView(R.id.iv_fold).setVisibility(View.GONE);
                helper.setText(R.id.tv_expand, "展开");
                ((TextView) helper.getView(R.id.tv_expand)).setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.mipmap.arrow_down_blue, 0);
            }
        });

        RecyclerView recyclerView = helper.getView(R.id.rv_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));

        if (!item.getOrder_list().isEmpty()) {
            recyclerView.setAdapter(new TradeHistoryRecordDetailAdapter(item.getOrder_list(), item.getScb().toUpperCase(), item.getXnb().toUpperCase(), item.getScb_cny_rate()));
        }


    }


    private class TradeHistoryRecordDetailAdapter extends BaseQuickAdapter<TradeCurrencyItemOrder, BaseViewHolder> {

        private String scb, xnb, rate;

        public TradeHistoryRecordDetailAdapter(@Nullable List<TradeCurrencyItemOrder> data, String scb, String xnb, String rate) {
            super(R.layout.tmtrade_layout_trade_history_detail_item, data);
            this.scb = scb;
            this.xnb = xnb;
            this.rate = rate;
        }

        @Override
        protected void convert(BaseViewHolder helper, TradeCurrencyItemOrder item) {

            helper.setText(R.id.tv_time, "成交时间：" + sdf.format(new Date(item.getAddtime() * 1000)));

            helper.setText(R.id.tv_price, new BigDecimal(item.getPrice()).setScale(6, BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString());
            helper.setText(R.id.tv_total_num, new BigDecimal(item.getNum()).setScale(6, BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString());
            helper.setText(R.id.tv_total_amount, new BigDecimal(item.getMum()).setScale(6, RoundingMode.DOWN).stripTrailingZeros().toPlainString());
            helper.setText(R.id.tv_fee, new BigDecimal(item.getFee()).setScale(6, BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString());
            helper.setText(R.id.tv_average_price, new BigDecimal(item.getPrice()).multiply(new BigDecimal(rate).setScale(2, RoundingMode.DOWN)).toPlainString());

            helper.setText(R.id.txt_price, "成交价格(" + scb + ")");
            helper.setText(R.id.txt_total_num, "成交量(" + xnb + ")");
            helper.setText(R.id.txt_total_amount, "成交额(" + scb + ")");
            helper.setText(R.id.txt_fee, "手续费(" + xnb + ")");
            helper.setText(R.id.txt_average_price, "成交价格(CNY)");

        }
    }


}
